//
//  ProductImageUpViewController.m
//  tangshantongjiAPP
//
//  Created by yuntai on 2018/4/4.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "ProductImageUpViewController.h"
#import "ProductDetailViewController.h"
#import <ZMJImageEditor/WBGImageEditor.h>

@interface ProductImageUpViewController ()<WBGImageEditorDelegate>
@property (weak, nonatomic) IBOutlet UILabel *imagenameLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftWithConstraint;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@end

@implementation ProductImageUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.imagenameLabel.textColor = kTextDeepDarkColor;
    
    UpLoadModelManager * manager = [UpLoadModelManager sharedInstance];
    if (manager.model.img1 == nil && manager.model.imageurl1 == nil) {
        /**第一步*/
        self.title = @"产品特征描述表照片";
        self.imagenameLabel.text = @"请拍摄产品特征描述表照片";
        self.rightBtn.hidden = NO;
        if (IS_IPHONE5) {
            self.leftWithConstraint.constant = 100;
        }else{
            self.leftWithConstraint.constant = (kScreenBoundWidth-20)/2;
        }
    }else{
        self.rightBtn.hidden = YES;
        self.leftWithConstraint.constant = kScreenBoundWidth-20;
        if (manager.model.img2 == nil) {
            self.title = @"当月报表照片";
            self.imagenameLabel.text = @"请拍摄当月报表照片";
        }else if (manager.model.img3 == nil){
            self.title = @"第一次价格采集凭证照片";
            self.imagenameLabel.text = @"请拍摄第一次价格采集原始凭证照片";
        }else if (manager.model.img4 == nil){
            self.title = @"第二次价格采集凭证照片";
            self.imagenameLabel.text = @"请拍摄第二次价格采集原始凭证照片";
        }else if (manager.model.img5 == nil){
            self.title = @"台账照片";
            self.imagenameLabel.text = @"请拍摄台账照片";
        }
    }
    
    weakself(self)
    [self setBackBtnEventHandler:^(id sender) {
        if ([UpLoadModelManager sharedInstance].popvc) {
            [weakself.navigationController popToViewController:[UpLoadModelManager sharedInstance].popvc  animated:YES];
        }
    }];
}

- (IBAction)upImageBtn:(UIButton *)sender {
    weakself(self)
    [[CameraTakeMamanger sharedInstance] cameraSheetInController:self handler:^(UIImage *image, NSString *imagePath) {
        [weakself selectImageMethodWithImage:image];
    } cancelHandler:^{
        
    }];
}
#pragma mark - 使用上月照片
- (IBAction)oldImageBtn:(UIButton *)sender {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[UpLoadModelManager sharedInstance].model.modelid?:@"" forKey:@"id"];
    [params setValue:@([UpLoadModelManager sharedInstance].model.type) forKey:@"type"];
    [params setValue:kTokenID forKey:@"token"];
    [HUDManager showLoadingHUDView:self.view];
    [NetWork PostNetWorkWithUrl:@"api/pro/saveProImg.ajax" with:params successBlock:^(NSDictionary *dic) {
        if ([dic[@"data"] isKindOfClass:[NSNull class]]|| dic[@"data"] == nil || [dic[@"data"] isEqualToString:@""]) {
            /**上个月无数据*/
            [HUDManager showWarningWithText:dic[@"message"]?:@""];

        }else{
            NSDictionary *tempdic = dic[@"data"];
            NSString *img1UrlStr = tempdic[@"img1"]?:@"";
            [UpLoadModelManager sharedInstance].model.imageurl1 = img1UrlStr;
            ProductImageUpViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ProductImageUpVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } FailureBlock:^(NSString *msg) {
        [HUDManager showWarningWithText:msg];
    } errorBlock:^(id error) {
        
    }];


}

#pragma mark ----private method

- (void)selectImageMethodWithImage:(UIImage *)tempImage{
    UpLoadModelManager * manager = [UpLoadModelManager sharedInstance];
    if (manager.model.img1== nil && manager.model.imageurl1 == nil) {
        /**第一步*/
        manager.model.img1 = tempImage;
        ProductImageUpViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ProductImageUpVC"];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        
        self.rightBtn.hidden = YES;
        self.leftWithConstraint.constant = kScreenBoundWidth-20;
        if (manager.model.img2 == nil) {
            manager.model.img2 = tempImage;
            ProductImageUpViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ProductImageUpVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (manager.model.img3 == nil){
            
            WBGImageEditor *editor = [[WBGImageEditor alloc] initWithImage:tempImage delegate:self];
            [self presentViewController:editor animated:YES completion:nil];
            
        }else if (manager.model.img4 == nil){
            WBGImageEditor *editor = [[WBGImageEditor alloc] initWithImage:tempImage delegate:self];
            [self presentViewController:editor animated:YES completion:nil];
        }else if (manager.model.img5 == nil){
            manager.model.img5 = tempImage;
            ProductDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"productdetailvc"];
            vc.isFirstUp = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
    
#pragma mark - WBGImageEditorDelegate
- (void)imageEditor:(WBGImageEditor *)editor didFinishEdittingWithImage:(UIImage *)image {
    
    UpLoadModelManager * manager = [UpLoadModelManager sharedInstance];
    if (manager.model.img3 == nil){
        manager.model.img3 = image;
    }else if ( manager.model.img4 == nil){
         manager.model.img4 = image;
    }
   
    ProductImageUpViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ProductImageUpVC"];
    [self.navigationController pushViewController:vc animated:YES];

    [editor.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
    
- (void)imageEditorDidCancel:(WBGImageEditor *)editor {
    
}
@end
